﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Effects.Effects.GuiParts
{
    /// <summary>
    ///  Class representing red LED diode.
    /// </summary>
    /// <remarks>
    ///  <para>
    ///   This is very simple control. It only work is to show wheter it is on or off by drawing one of images. 
    ///   So it has only one property for setting on/off state.
    ///  </para>
    /// </remarks>
    public partial class LED : UserControl
    {
        #region turning_on_off
        // turing the LED On/Off

        /// <summary>
        ///  on/off state. True means on.
        /// </summary>
        private bool on;

        /// <summary>
        ///  Gets or sets the on/off state.
        /// </summary>
        public bool On
        {
            get { return on; }
            set
            {
                if (on ^ value)
                {
                    on = value;
                    RefreshMultiThread();
                }
            }
        }

        #endregion

        #region constructor
        // constructor

        /// <summary>
        ///  Creates new instance of LED.
        /// </summary>
        public LED()
        {
            InitializeComponent();
            refreshInvoker = new MethodInvoker(Refresh);
            this.BackColor = Color.Transparent;

            //onImage = Image.FromStream(new System.IO.MemoryStream(global::GUI.Properties.Resources.LEDon));
            //offImage = Image.FromStream(new System.IO.MemoryStream(global::GUI.Properties.Resources.LEDoff));
            onImage = Image.FromStream(new System.IO.MemoryStream(global::Effects.GuiParts.Resources.LED.Led_On));
            offImage = Image.FromStream(new System.IO.MemoryStream(global::Effects.GuiParts.Resources.LED.Led_Off));

            this.Size = onImage.Size;

            // set important style constants
            SetStyle(ControlStyles.AllPaintingInWmPaint |
                          ControlStyles.UserPaint |
                          ControlStyles.DoubleBuffer, true);
        }

        #endregion

        #region painting_control
        // appereance stuff

        /// <summary>
        ///  Appearance when LED is on.
        /// </summary>
        private Image onImage;

        /// <summary>
        ///  Appearance when LED is off.
        /// </summary>
        private Image offImage;


        /// <summary>
        ///  Draws the component.
        /// </summary>
        /// <param name="e"> Paint event arguments. </param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;

            if (on)
                g.DrawImage(onImage, 0, 0);
            else
                g.DrawImage(offImage, 0, 0);
        }
        #endregion

        #region refresh_multi_thread
        // refresh control from all threads

        /// <summary>
        ///  Invoker for Refresh method.
        /// </summary>
        protected MethodInvoker refreshInvoker;

        /// <summary>
        ///  Refresh method accessible from all threads
        /// </summary>
        protected void RefreshMultiThread()
        {
            if (InvokeRequired)
                BeginInvoke(refreshInvoker);
            else
                Refresh();
        }

        #endregion
    }
}
